<?php

/**
 * 后台模块
 * @copyright (c) 2013, Mr.Wang
 * 
 */
class AdminAction extends AdminCommonAction {

    public function _initialize() {
        parent::_initialize();
        //排除不用验证的模块
        if (!isset($_SESSION[C('USER_AUTH_KEY')]) && C('NOT_AUTH_MODULE') != MODULE_NAME) {
            $this->redirect(U("Admin/Public/Login"));
        } else {
            G::$Cache['uid'] = $_SESSION['admin_uid'];
            G::$Cache['username'] = $_SESSION['AdminInfo']['username'];
            G::$Cache['roleid'] = 1;
        }
        //初始化站点配置信息
        $this->_config();
        if (method_exists($this, "_init")) {
            $this->_init();
        }
    }

    /**
     * 所有配置参数
     */
    final private function _config() {
        $config = D("Config");
        $list   = $config->getField('varname,value');
        if (!empty($list) && is_array($list)) {
            foreach ($list as $k => $v) {
                C(strtoupper($k), $v);
            }
        }

        $this->assign("config_siteurl", $list['siteurl']);
        $this->assign('waitSecond', 5000);
        $this->assign("Config", $list);
    }

    /**
     * 获取菜单导航
     * @param type $app
     * @param type $model
     * @param type $action
     */
    public static function getMenu() {

        $menuid = (int) $_GET['menuid'];
        $menuid = $menuid ? $menuid : cookie("menuid", "", array("prefix" => ""));
        //cookie("menuid",$menuid);

        $db   = D("Menu");
        $info = $db->cache(true, 60)->where(array("id"  => $menuid))->getField("id,action,app,model,parentid,data,type,name");
        $find = $db->cache(true, 60)->where(array("parentid" => $menuid, "status"   => 1))->getField("id,action,app,model,parentid,data,type,name");

        if ($find) {
            array_unshift($find, $info[$menuid]);
        } else {
            $find = $info;
        }
        foreach ($find as $k => $v) {
            $find[$k]['data'] = "menuid=$menuid&" . $find[$k]['data'];
        }

        return $find;
    }

    /**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @return void
     */
    protected function ajaxReturn($data, $type = '') {
        if (func_num_args() > 2) {// 兼容3.0之前用法
            $args = func_get_args();
            array_shift($args);
            $info = array();
            $info['data']   = $data;
            $info['info']   = array_shift($args);
            $info['status'] = array_shift($args);
            $data           = $info;
            $type           = $args ? array_shift($args) : '';
        }
        //返回格式
        $return         = array(
            //跳转地址
            "referer" => $data['url'] ? $data['url'] : "",
            //提示类型，success fail
            "state"   => $data['status'] ? "success" : "fail",
            //提示内容
            "info"    => $data['info'],
            "status"  => $data['status'],
            //数据
            "data"    => $data['data'],
        );
        if (empty($type))
            $type     = C('DEFAULT_AJAX_RETURN');
        switch (strtoupper($type)) {
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:text/html; charset=utf-8');
                exit(json_encode($return));
            case 'XML' :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($return));
            case 'JSONP':
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
                exit($handler . '(' . json_encode($return) . ');');
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($return);
            default :
                // 用于扩展其他返回格式数据
                tag('ajax_return', $return);
        }
    }
    
    
    
    
    Public function upload($savePath = './Public/Uploads/') {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        if (strpos($savePath, ".") !== 0) {
            $savePath = "." . $savePath;
        }


        if (!is_dir($savePath)) {
            createDir($savePath);
        }
        $upload->savePath = $savePath; // 设置附件上传目录
        if (!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        } else {// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            $list = array();
            foreach ($info as $value) {
                $list[$value['key']] = $value;
            }
            return $list;
        }
    }

}
